import Vue from 'vue'
import Vuex from 'vuex'
import { getSession, setSession } from '@utils'
import $api from '@/api'

Vue.use(Vuex)
export default new Vuex.Store({
  state: {
    currentAMenu: getSession('aMenu'),
    currentUser: getSession('currentUser'),
    currentBook: getSession('currentBook'),
    currentChapter: getSession('currentChapter'),
    currentPost: getSession('currentPost'),
    allCategory: getSession('allCategory'),
    allFile: getSession('allFile'),
    allBook: getSession('allBook'),
    allUser: getSession('allUser'),
    allItem: getSession('allItem'),
    myCart: getSession('myCart'),
    userOrder: getSession('userOrder'),
    myBorrow: getSession('myBorrow')
  },
  getters: {
    getCategoryById: (state) => (id) => {
      return state.allCategory && state.allCategory.find((item) => item.id === id)
    },
    getFileById: (state) => (id) => {
      return state.allFile.find((item) => item.userId === id)
    },
    getMiniBook: (state) => () => {
      return state.allBook && state.allBook.map(({ id, name }) => ({ id, name }))
    },
    getBookById: (state) => (id) => {
      return state.allBook && state.allBook.find((i) => i.id === id)
    },
    getUserById: (state) => (id) => {
      return state.allUser && state.allUser.find((item) => item.id === id)
    },
    getMiniCategory: (state) => () => {
      return state.allCategory?.filter(({ id, name, pid }) => {
        return { id, name, pid }
      })
    }
  },
  mutations: {
    setAMenu(state, data) {
      state.currentAMenu = data
      setSession('aMenu', data)
    },
    setCurrentUser(state, data) {
      state.currentUser = data
      setSession('currentUser', data)
    },
    setCurrentBook(state, data) {
      state.currentBook = data
      setSession('currentBook', data)
    },
    setCurrentChapter(state, data) {
      state.currentChapter = data
      setSession('currentChapter', data)
    },
    setCurrentPost(state, data) {
      state.currentPost = data
      setSession('currentPost', data)
    },
    setAllCategory(state, data) {
      state.allCategory = data
      setSession('allCategory', data)
    },
    setAllFile(state, data) {
      state.allFile = data
      setSession('allFile', data)
    },
    setAllBook(state, data) {
      state.allBook = data
      setSession('allBook', data)
    },
    setAllUser(state, data) {
      state.allUser = data
      setSession('allUser', data)
    },
    setMyCart(state, data) {
      const myCart = data.filter((i) => {
        return i.userId === state.currentUser.id.toString()
      })
      setSession('myCart', myCart)
      state.myCart = myCart
    },
    setUserOrder(state, data) {
      state.userOrder = data
      setSession('userOrder', data)
    },
    setMyBorrow(state, data) {
      state.myBorrow = data
      setSession('myBorrow', data)
    },
    clearInfo(state) {
      sessionStorage.clear()
      state.currentUser = null
    }
  },
  actions: {
    async getAllCategory({ commit }) {
      const data = await $api.getCategory()
      commit('setAllCategory', data)
    },
    async getAllFile({ commit }) {
      const data = await $api.getFile()
      commit('setAllFile', data)
    },
    async getAllBook({ commit }) {
      const data = await $api.getBook()
      commit('setAllBook', data)
    },
    async getAllUser({ commit }) {
      const data = await $api.getUser()
      commit('setAllUser', data)
    },
    async fetchAllItem({ commit }) {
      const res = await $api.getItem()
      console.log(res)
      // commit('setAllItem', data)
    },
    async fetchAllNotice({ commit }) {
      const { data } = await $api.getNotices()
      commit('setAllNotice', data)
    },
    async fetchMyCart({ commit }) {
      const { data } = await $api.getShoppingCar()
      commit('setMyCart', data)
    },
    async fetchUserOrder({ commit, state }) {
      const { list } = await $api.getOrderByUser({ keyword: state.currentUser.id, size: 999 })
      commit('setUserOrder', list)
    },
    async fetchMyBorrow({ commit, state }) {
      const { list } = await $api.getBorrowList({ keyword: state.currentUser.id, size: 999 })
      console.log('list :', list)
      commit('setMyBorrow', list)
    }
  }
})
